package com.ctsi.framework.schema.manager.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: zhangjw
 * @Date: 2018/10/19 09:42
 * @Description:
 */
@Deprecated
public class ChangeLogComparator implements Comparator<String> {

    private static final Logger logger = LoggerFactory.getLogger(ChangeLogComparator.class);

    private static final Map<String,Integer> map = new HashMap<>();

    private static final String reg = ".+\\.(.*)\\.";

    private AtomicInteger ai = new AtomicInteger(0);

    static {
        map.put("table",0);
        map.put("index",1);
        map.put("view",2);
        map.put("data",3);
        map.put("project",4);
    }

    /**
     *  liquibase使用treeset排序，为保证数据不丢失，不能返回0
     *  2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/offlinesupport-app-rest-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/offlinesupport-app-rest-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/uaa-engine-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/offlinesupport-app-rest-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/system-engine-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/offlinesupport-app-rest-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/system-engine-3.1.0-SNAPSHOT.jar!/initdata/
     * 2018-10-30 11:23:05 WARN  [main] com.ctsi.framework.schema.manager.impl.ChangeLogComparator 41 getOrder  - ??????--jar:file:/app.jar!/BOOT-INF/lib/uaa-engine-3.1.0-SNAPSHOT.jar!/initdata/
     * @param uri
     * @return
     */
    private int getOrder(String uri){
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(uri);
        logger.warn("loading schema resources:--{}",uri);

        if (m.find()) {
            if (map.containsKey(m.group(1))) {
                return map.get(m.group(1));
            } else {
                return ai.getAndIncrement();
            }
        }
        return ai.getAndIncrement();
    }


    @Override
    public int compare(String o1, String o2) {
        if (getOrder(o1) == getOrder(o2)) {
            return 1;
        }
        return getOrder(o1) - getOrder(o2);
    }

}
